A suite of tools to scrape and parse search engine results

您所在的位置:网站首页 python format !r A suite of tools to scrape and parse search engine results

A suite of tools to scrape and parse search engine results

#A suite of tools to scrape and parse search engine results | 来源: 网络整理| 查看: 265

[This article was first published on schochastics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. ShareTweet

My posts are usually R only. But in this post, I want to talk about a suite of tools developed by my colleagues and me that goes beyond R only. This suite of tools helps to gather results from different search engines and includes a browser extension to scrape the results, and a Python library and an R package to parse the results.

The browser extension

The core tool is a browser extension for Mozilla and Chrome that simulates a user searching (at least) 50-top main, news, images and videos search results of up to 8 different search engines.

The repository is well documented and walks you through the steps of setting up the extension to scrape data. Always make sure to deactivate the extension once you are done since it interferes with your normal browsing.

The extension can store local snapshots of the html files of the search results for later analysis. This is done via the web extension SingleFile. To parse these html files efficiently for important information, we a Python library as well as an R package.

The Python library

To use the Python library, simply clone the repository and either add webbotparser/webbotparser.py to your working directory or navigate to the folder, and run

pip install -e .

The webbotparser is then available in your Python installation.

(The following is an excerpt of the repository README)

Usage

For the search engines and result types supported out of the box, simply run

from webbotparser import WebBotParser

and initialize the WebBotParser for the search engine and result type your are investigating, for example

parser = WebBotParser(engine = 'DuckDuckGo News')

Then, you can obtain the search results as a pandas DataFrame and metadata as a Python dictionary with

metadata, results = parser.get_results(file='path/to/the/result_page.html')

Furthermore, parser.get_metadata(file) can be used to only extract the metadata. parser.get_results_from_dir(dir) allows to directly extract search results spread over multiple pages, as Google text result are provided for instance. For examples also see example.ipynb.

Extracting images

WebBot archives images inline in the html file of the search results, i.e., they are neither external files on your drive nor fetched from the original source on viewing the downloaded search results page. This allows us to extract the images directly from the html file for further analysis. The engines and result types supported out of the box with WebBotParser allow for extracting images as well. Simply initialize WebBotParser as follows:

parser = WebBotParser(engine = 'Google Video', extract_images=True)

You can optionally specify extract_images_prefix, extract_images_format, and extract_images_to_dir. See example.ipynb for more details, including preview in Jupyter Notebooks.

Custom result types

WebBotParser out of the box only provides support for some search engines and result types. Even these parsers might stop working if the search engine providers decide to change their layout. However, WebBotParser can still be used in these cases by defining a custom result_selector, queries, and optionally a metadata_extractor function. In this case, a WebBotParser is initiated with these instead of with the engine attribute

parser = WebBotParser(queries, result_selector, metadata_extractor)

Under the hood, WebBotParser uses BeautifulSoup to

Parse the search result page’s HTML via LXML Disciminate the individual results on each page using a CSS selector called result_selector that matches a list of search results For each of those results, extract available information through a list of queries

See the below example for available types of queries and their usage

queries = [ # extract the text from inside a matched element, getting all the text over all its children {'name': 'abc', 'type': 'text', 'selector': 'h3'}, # extract the value of an attribute of a matched element {'name': 'def', 'type': 'attribute', 'selector': 'a', 'attribute': 'href'}, # whether or not a CSS selector matches, returns a Boolean {'name': 'ghi', 'type': 'exists', 'selector': 'ul'}, # extract inline images and name them by a title {'name': 'jkl', 'type': 'image', 'selector': 'g-img > img', 'title_selector': 'h3'} # pass a custom query function {'name': 'mno', 'type': 'custom', 'function': my_function}, ]

You can optionally provide a metadata_extractor(soup, file) function to extract metadata alongside the search results, or import one of the existing extractors, e.g. with

from webbotparser import GoogleParser metadata_extractor = GoogleParser.google_metadata The R package

The R package can be installed from GitHub

remotes::install_github("schochastics/webbotparseR") library(webbotparseR)

The package contains an example html from a google search on climate change.

ex_file


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3